package com.runen.infinitemovement.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.ColumnDetailsActivity;
import com.runen.infinitemovement.adapter.ColumnAdapter;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.entity.CollageInfoEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LongMarchStoryFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.rv_long_march_story)
    RecyclerView rvLongMarchStory;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private View view;
    private ColumnAdapter columnAdapter;
    private int page = 2;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_long_march_story, container, false);
            ButterKnife.bind(this, view);
            initView();
            initData();
        }

        return view;
    }

    private void initData() {
        srl.setRefreshing(true);
        String string = "?limit=10&page=1";
        new ActivityAPI().getMyLongCollageInfo(string, new BaseResultCallback<CollageInfoEntity>() {
            @Override
            public void onResponse(CollageInfoEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null) {
                        columnAdapter.setNewData(response.data.informationList);
                        if (columnAdapter.getData().size() >= response.data.count) {
                            columnAdapter.getLoadMoreModule().loadMoreEnd(true);
                            columnAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }
                    }
                }

            }
        });
    }

    private void initView() {
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvLongMarchStory.setLayoutManager(layoutManager);
        columnAdapter = new ColumnAdapter(getActivity());
        columnAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvLongMarchStory.setAdapter(columnAdapter);

        columnAdapter.getLoadMoreModule().setAutoLoadMore(false);
        columnAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), ColumnDetailsActivity.class);
                intent.putExtra("informationId", columnAdapter.getData().get(position).informationId);
                startActivity(intent);
            }
        });
        initLoadMore();
    }
    private void initLoadMore() {
        columnAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        columnAdapter.getLoadMoreModule().setAutoLoadMore(true);
        columnAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }
    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    private void loadMoreRequested() {
        String string = "?limit=10&page=%s";
        new ActivityAPI().getMyLongCollageInfo(String.format(string, page), new BaseResultCallback<CollageInfoEntity>() {
            @Override
            public void onResponse(CollageInfoEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.informationList.size() > 0) {
                        columnAdapter.addData(response.data.informationList);
                        if (columnAdapter.getData().size() >= response.data.count) {
                            columnAdapter.getLoadMoreModule().loadMoreEnd(true);
                            columnAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            columnAdapter.getLoadMoreModule().loadMoreComplete();
                            columnAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                }

            }
        });
    }
}
